import * as THREE from "three";

export const StoreShelf=(option)=>{
  let binLength = option.bin_length||50;//库位长度
  let binWidth = option.bin_width||50;//库位宽
  let binHeight = option.bin_height||50;//库位高
  let binXNum = option.bin_xnum||1;//库位X轴方向库位数量
  let binZNum = option.bin_znum||10;//库位Z轴方向库位数量
  let binYNum = option.bin_ynum||10;//库位Y轴库位数量
  let bottomHight = option.botton_height||20;//底层高度，底层
  let positionX = option.position.x||0;//库位位置
  let positionY = option.position.y||0;//库位位置
  let positionZ = option.position.z||0;//库位位置
  let rackLengh = 3;//支架的长度，默认设动为3
  let rackWidth = 3;//支架的宽度，默认设定为3
  let intervalRackNum=2;//间隔多少库位有一个主支架\
  let binHolderPlane=new THREE.BoxGeometry(binLength,2,binWidth);//定义一个跟库位长宽相同的几何体，作为托盘
  //定义支架和托盘的材质
  let shlefMat = new THREE.MeshLambertMaterial({color:0x175EC0});//定义支架和托盘的材质
  let group = new THREE.Group();//定义一个组合体
  //使用marge合并模型提升流畅度
  let combineGeometry =new THREE.Geometry();
  /*
   支架的高=最底层的高度+(库位数-1)*库位的高度
   */
  let rackHeight=bottomHight+(binYNum-1)*binHeight;
  /*
   支架的宽=库位数乘以库位的宽度
   */
  let shelfWidth=binZNum*binWidth;
  let rackBoxGeometry=new THREE.BoxGeometry(rackLengh,rackHeight,rackWidth);//定义一个支架网格
  let rackObject=new THREE.Mesh( rackBoxGeometry, shlefMat, 0 );

  let plane = new THREE.BoxGeometry(binLength, 2,binWidth);
  //托盘对象
  let holderObject = new THREE.Mesh(plane, shlefMat,0);

  let leftPositionX=positionX-binLength/2+rackLengh/2;//左侧支架柱的X轴条码
  let rightPositionX=positionX+binLength/2-rackLengh/2;//右侧支架柱的X轴条码
  let YY=positionY-binHeight/2;//支架柱的Y轴坐标
  for(let i=0;i<binZNum+intervalRackNum;i++)
  {
    let isRack=i%intervalRackNum;
    if(isRack===0)
    {
      let PositionZ=positionZ-shelfWidth/2+i*binWidth+rackWidth/2;
      if(i>=binZNum)
      {
        PositionZ=PositionZ-rackWidth;
      }

      let leftRack=rackObject.clone();
      leftRack.position.set(leftPositionX,YY,PositionZ);
      leftRack.updateMatrix();
      combineGeometry.merge(leftRack.geometry, leftRack.matrix);
      let rightRack=rackObject.clone();
      rightRack.position.set(rightPositionX,YY,PositionZ);
      rightRack.updateMatrix();
      combineGeometry.merge(rightRack.geometry, rightRack.matrix);
    }
  }

  //创建托板
  for(let i=0;i<binZNum;i++)
  {
    for (let j = 0; j < binYNum; j++) {
      let Y=positionY-binHeight/2-rackHeight/2+bottomHight+j*binHeight;
      let Z=positionZ-shelfWidth/2+binWidth/2 + i * binWidth
      let holderObj= holderObject.clone();
      holderObj.position.set(positionX,Y,Z);
      holderObj.updateMatrix();
      combineGeometry.merge(holderObj.geometry, holderObj.matrix);
    }
  }
  let shelf= new THREE.Mesh(combineGeometry, shlefMat);
  shelf.uuid=option.no;
  shelf.name=option.group_name;
  shelf.type="StoreShelf";
  return shelf;


}
